//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "YoYOptionletStripper.h"
using namespace Cephei::QL::Experimental::Inflation;
#include <gen/QL/Experimental/Inflation/YoYCapFloorTermPriceSurface.h>
#include <gen/QL/Pricingengines/Inflation/YoYInflationCapFloorEngine.h>
using namespace Cephei::QL::Pricingengines::Inflation;
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Inflation::CYoYOptionletStripper::CYoYOptionletStripper (boost::shared_ptr<QuantLib::YoYOptionletStripper>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phYoYOptionletStripper = NULL;
#endif
	_ppYoYOptionletStripper = &childNative;
    
}
Cephei::QL::Experimental::Inflation::CYoYOptionletStripper::CYoYOptionletStripper (QuantLib::YoYOptionletStripper& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phYoYOptionletStripper = NULL;
#endif
	_ppYoYOptionletStripper = new boost::shared_ptr<QuantLib::YoYOptionletStripper> (&childNative);
    
    _YoYOptionletStripperOwner = owner;
    
}

Cephei::QL::Experimental::Inflation::CYoYOptionletStripper::CYoYOptionletStripper (CYoYOptionletStripper^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phYoYOptionletStripper = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppYoYOptionletStripper = new boost::shared_ptr<QuantLib::YoYOptionletStripper> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Inflation::CYoYOptionletStripper::CYoYOptionletStripper (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phYoYOptionletStripper = NULL;
#endif
	if (!t->IsSubclassOf(CYoYOptionletStripper::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Inflation::CYoYOptionletStripper::CYoYOptionletStripper (QuantLib::Handle<QuantLib::YoYOptionletStripper>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phYoYOptionletStripper = &childNative;
	_ppYoYOptionletStripper = &static_cast<boost::shared_ptr<QuantLib::YoYOptionletStripper>>(childNative.currentLink());
    
    _YoYOptionletStripperOwner = owner;
}
Cephei::QL::Experimental::Inflation::CYoYOptionletStripper::CYoYOptionletStripper (QuantLib::Handle<QuantLib::YoYOptionletStripper> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phYoYOptionletStripper = &childNative;
	_ppYoYOptionletStripper = &static_cast<boost::shared_ptr<QuantLib::YoYOptionletStripper>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Inflation::CYoYOptionletStripper::CYoYOptionletStripper (QuantLib::YoYOptionletStripper childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phYoYOptionletStripper = NULL;
#endif
	_ppYoYOptionletStripper = new boost::shared_ptr<QuantLib::YoYOptionletStripper> (new QuantLib::YoYOptionletStripper (childNative));
    
}
#endif

Cephei::QL::Experimental::Inflation::CYoYOptionletStripper::~CYoYOptionletStripper ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppYoYOptionletStripper != NULL)
    {
	    delete _ppYoYOptionletStripper;
        _ppYoYOptionletStripper = NULL;
    }
}
Cephei::QL::Experimental::Inflation::CYoYOptionletStripper::!CYoYOptionletStripper ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppYoYOptionletStripper != NULL)
    {
	    delete _ppYoYOptionletStripper;
    }
}
QuantLib::YoYOptionletStripper& Cephei::QL::Experimental::Inflation::CYoYOptionletStripper::GetReference ()
{
    if (_ppYoYOptionletStripper == NULL) throw REFNEW NativeNullException ();
	return **_ppYoYOptionletStripper;
}
boost::shared_ptr<QuantLib::YoYOptionletStripper>& Cephei::QL::Experimental::Inflation::CYoYOptionletStripper::GetShared ()
{
    if (_ppYoYOptionletStripper == NULL) throw REFNEW NativeNullException ();
	return *_ppYoYOptionletStripper;
}
QuantLib::YoYOptionletStripper* Cephei::QL::Experimental::Inflation::CYoYOptionletStripper::GetPointer ()
{
    if (_ppYoYOptionletStripper == NULL) throw REFNEW NativeNullException ();
	return &**_ppYoYOptionletStripper;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::YoYOptionletStripper>& Cephei::QL::Experimental::Inflation::CYoYOptionletStripper::GetHandle ()
{
	if (_phYoYOptionletStripper == NULL)
	{
		_phYoYOptionletStripper = new Handle<QuantLib::YoYOptionletStripper> (*_ppYoYOptionletStripper);
	}
	return *_phYoYOptionletStripper;
}
#endif
bool Cephei::QL::Experimental::Inflation::CYoYOptionletStripper::HasNative () 
{
	return (_ppYoYOptionletStripper != NULL);
}

Cephei::QL::Experimental::Inflation::IYoYOptionletStripper^ Cephei::QL::Experimental::Inflation::CYoYOptionletStripper::Initialize (Cephei::QL::Experimental::Inflation::IYoYCapFloorTermPriceSurface^ prm1, Cephei::QL::Pricingengines::Inflation::IYoYInflationCapFloorEngine^ prm2, Double slope)
{
    CYoYCapFloorTermPriceSurface^ _Cprm1;
    CYoYInflationCapFloorEngine^ _Cprm2;
    try
    {
        _Cprm1 = safe_cast<CYoYCapFloorTermPriceSurface^> (prm1);
        _Cprm1->Lock();
        boost::shared_ptr<QuantLib::YoYCapFloorTermPriceSurface>& _prm1 = static_cast<boost::shared_ptr<QuantLib::YoYCapFloorTermPriceSurface>&> (_Cprm1->GetShared ()); 
        _Cprm2 = safe_cast<CYoYInflationCapFloorEngine^> (prm2);
        _Cprm2->Lock();
        boost::shared_ptr<QuantLib::YoYInflationCapFloorEngine>& _prm2 = static_cast<boost::shared_ptr<QuantLib::YoYInflationCapFloorEngine>&> (_Cprm2->GetShared ()); 
        QuantLib::Real _slope = (QuantLib::Real)ValueHelper::Convert (slope); //a
    	(*_ppYoYOptionletStripper)->initialize ( _prm1,  _prm2,  _slope );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
        if (_Cprm2 != nullptr) _Cprm2->Unlock();
    }
}
Double Cephei::QL::Experimental::Inflation::CYoYOptionletStripper::MaxStrike::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppYoYOptionletStripper)->maxStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Inflation::CYoYOptionletStripper::MinStrike::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppYoYOptionletStripper)->minStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Experimental::Inflation::CYoYOptionletStripper::Strikes::get ()
{
    try
    {
    	std::vector<QuantLib::Rate> _rv = (std::vector<QuantLib::Rate>)(*_ppYoYOptionletStripper)->strikes ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

